﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DBQLTV;

namespace QuanLyThuVien
{
    public partial class frmPhieuTraSach : Form
    {
        CuonSachBUS cuonSachBUS = new CuonSachBUS();
        DocGiaBUS docGiaBUS = new DocGiaBUS();
        int maDG;
        int tienMoiTra = 0;

        public frmPhieuTraSach()
        {
            InitializeComponent();
            tb_tienTraKyNay.Text = "0";
            List<DOCGIA> listDG = docGiaBUS.GetListDocGia();
            foreach (DOCGIA dg in listDG)
            {
                comboBox_maDG.Items.Add(dg.MaDG);
            }
        }
        private void UpdateDatatable()
        {
            int _stt = 0;
            int tienPhatKyNay = 0;
            maDG = int.Parse(comboBox_maDG.Text);

            List<int> listCuonSachMuon = cuonSachBUS.GetListMaCuonSachMuonBoiDocGia(maDG);

            dataGridView_traSach.RowCount = 1;
            dataGridView_traSach.Rows.Clear();

            if (listCuonSachMuon.Count == 0)
                MessageBox.Show("Độc giả không giữ cuốn sách nào.");
            foreach (int temp in listCuonSachMuon)
            {
                _stt++;
                dataGridView_traSach.Rows.Add(_stt, temp, cuonSachBUS.GetCuonSachByKey(temp).NgayMuon, cuonSachBUS.GetSoNgayMuon(temp), cuonSachBUS.GetTienPhat(temp));
                tienPhatKyNay += cuonSachBUS.GetTienPhat(temp);
            }
            tb_tienPhatKyNay.Text = tienPhatKyNay.ToString();

            if (tb_tienTraKyNay.Text != "")
            {
                tienMoiTra = int.Parse(tb_tienTraKyNay.Text);

            }
            else tienMoiTra = 0;
            tb_tongNo.Text = (docGiaBUS.GetDocGia(maDG).TongNo - tienMoiTra).ToString();

        }
        private void button_luu_Click(object sender, EventArgs e)
        {
            PhieuTraSachBUS phieuTraBUS = new PhieuTraSachBUS();
            CTPhieuTraSachBUS cTPhieuTra = new CTPhieuTraSachBUS();

            List<int> listMaSachTra = new List<int>();
            for (int i = 0; i < dataGridView_traSach.Rows.Count; i++)
            {
                DataGridViewCheckBoxCell chk = dataGridView_traSach.Rows[i].Cells[5] as DataGridViewCheckBoxCell;
                if (Convert.ToBoolean(chk.Value) == true)
                {
                    if (dataGridView_traSach[1, i].Value.ToString() != null)
                        listMaSachTra.Add(int.Parse(dataGridView_traSach[1, i].Value.ToString()));
                }
            }

            if (listMaSachTra.Count != 0)
            {
                //temp
                int va1 = int.Parse(tb_tienPhatKyNay.Text);
                int va2 = va1 - int.Parse(tb_tienTraKyNay.Text);
                string va4 = dateTimePicker_ngayTra.Value.ToString();

                int maPhieuTra = phieuTraBUS.InsertPhieuTraSach(va1, va2, maDG, va4);
                foreach (int temp in listMaSachTra)
                {
                    cTPhieuTra.InsertCTPhieuTraSach(maPhieuTra, temp);
                }
                MessageBox.Show("Thành công!");
                this.Close();
            }
            else MessageBox.Show("Chưa trả sách nào", "Lỗi");
        }
        private void button_huy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tb_tienTraKyNay_KeyUp(object sender, KeyEventArgs e)
        {
            if (tb_tienTraKyNay.Text != "")
                tienMoiTra = int.Parse(tb_tienTraKyNay.Text);
            else tienMoiTra = 0;
            tb_tongNo.Text = (docGiaBUS.GetDocGia(maDG).TongNo - tienMoiTra).ToString();
        }
        private void tb_HoTen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        private void comboBox_maDG_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }     
        private void comboBox_maDG_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateDatatable();
        }
        private void comboBox_maDG_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            int maDG = int.Parse(comboBox_maDG.SelectedItem.ToString());
            tb_HoTen.Text = docGiaBUS.GetDocGia(int.Parse(comboBox_maDG.Text)).Ten;

            UpdateDatatable();
        }
        private void comboBox_maDG_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!comboBox_maDG.Items.Contains(int.Parse(comboBox_maDG.Text)))
                    MessageBox.Show("Mã độc giả không tồn tại!", "Lỗi");
                else UpdateDatatable();
            }
        }
    }
}
